package com.nuoyuan.wx.admin.controller;

import com.nuoyuan.wx.common.controller.BaseController;
import com.nuoyuan.wx.common.dto.BaseRespDto;
import com.nuoyuan.wx.common.utils.GsonUtil;
import com.nuoyuan.wx.service.ProductService;
import com.nuoyuan.wx.vo.FileVo;
import com.nuoyuan.wx.vo.PageVo;
import com.nuoyuan.wx.vo.ProductVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;

/**
 * @Package com.nuoyuan.wx.admin.controller
 * @ClassName ProductController
 * @Description TODO
 * @Author linus
 * @Date 2020-02-13 14:19
 * @Version 1.0
 **/
@RestController("AdminProductController")
public class ProductController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(ProductController.class);
    @Autowired
    private ProductService productService;

    /**
     * 新增
     * @param param
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/admin/product", method = RequestMethod.POST)
    @ResponseBody
    public String add(@RequestBody ProductVo param, HttpServletRequest request, HttpServletResponse response) {
        BaseRespDto resp = productService.add(param);
        if(!resp.isSuccess()) {
            log.error(resp.getMsg());
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return this.buildErrorResp(resp.getMsg());
        }

        return this.buildSimpleSuccResp();
    }

    /**
     * 更新上下架
     * @param param
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/admin/product/sale", method = RequestMethod.POST)
    @ResponseBody
    public String sale(@RequestBody ProductVo param, HttpServletRequest request, HttpServletResponse response) {
        BaseRespDto resp = productService.update(param);
        if(!resp.isSuccess()) {
            log.error(resp.getMsg());
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return this.buildErrorResp(resp.getMsg());
        }

        return this.buildSimpleSuccResp();
    }



    /**
     * 更新
     * @param param
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/admin/productUpdate", method = RequestMethod.POST)
    @ResponseBody
    public String update(@RequestBody ProductVo param, HttpServletRequest request, HttpServletResponse response) {
        BaseRespDto resp = productService.update(param);
        if(!resp.isSuccess()) {
            log.error(resp.getMsg());
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return this.buildErrorResp(resp.getMsg());
        }

        return this.buildSimpleSuccResp();
    }

    /**
     * 列表
     * @param page
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/admin/products", method = RequestMethod.GET)
    @ResponseBody
    public String list(PageVo page, HttpServletRequest request, HttpServletResponse response) {
        BaseRespDto<PageVo<ProductVo>> resp = productService.getList(page);
        if(!resp.isSuccess()) {
            log.error(resp.getMsg());
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return this.buildErrorResp(resp.getMsg());
        }
        return GsonUtil.toJson(resp.getData());
    }


    /**
     * 详情
     * @param param
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/admin/product", method = RequestMethod.GET)
    @ResponseBody
    public String detail(ProductVo param, HttpServletRequest request, HttpServletResponse response) {
        BaseRespDto<ProductVo> resp = productService.getDetail(param);
        if(!resp.isSuccess()) {
            log.error(resp.getMsg());
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return this.buildErrorResp(resp.getMsg());
        }

        return GsonUtil.toJson(resp.getData());
    }

    /**
     * 上传文件
     * @param param
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/admin/product/upload", method = RequestMethod.POST)
    @ResponseBody
    public String upload(FileVo param, HttpServletRequest request, HttpServletResponse response) {
        BaseRespDto<String> resp = productService.upload(param);
        if(!resp.isSuccess()) {
            log.error(resp.getMsg());
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return this.buildErrorResp(resp.getMsg());
        }
        param.setFileName(param.getFile().getOriginalFilename());
        param.setFileUrl(resp.getData());
        return GsonUtil.toJson(param);
    }
}
